package com.admin.scroller.photoorvideo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.ViewGroup;

import com.admin.scroller.photoorvideo.PerivewActivity;
import com.admin.scroller.photoorvideo.PhotoChoseActivity;
import com.admin.scroller.photoorvideo.beans.DatePhoto;
import com.admin.scroller.photoorvideo.holders.ChildGridHolder;
import com.admin.scroller.photoorvideo.holders.PhotosHolder;
import com.admin.scroller.photoorvideo.beans.LocalPhoto;
import com.admin.scroller.photoorvideo.holders.TakenPhotosHolder;

import java.util.List;

/**
 * 列表展示fragment的列表适配器
 */
public class PhotosAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<DatePhoto> photoList;

    private Context context;

    public PhotosAdapter(Context context){
        this.context=context;
    }

    public void upData(List<DatePhoto> photoList){
        this.photoList=photoList;
        //预留的拍照按钮位置
        if(!TextUtils.isEmpty(photoList.get(0).getDate())) {
            this.photoList.add(0, new DatePhoto());
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==0){
            return TakenPhotosHolder.getHolder(context,parent,viewType);
        }
        PhotosHolder holder=PhotosHolder.getHolder(context,parent,viewType);
        holder.setOnImageItemClick(onImageItemClick);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof PhotosHolder) {
            ((PhotosHolder)holder).initData(photoList.get(position),position);
        }
    }

    @Override
    public int getItemCount() {
        return photoList==null?0:photoList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return 0;
        }
        return 1;
    }

    public DatePhoto getItem(int position){
        return photoList.get(position);
    }

    private ChildGridHolder.OnImageItemClick onImageItemClick=new ChildGridHolder.OnImageItemClick() {
        @Override
        public void onItemSelected(int persent,int position) {
            LocalPhoto localPhoto=photoList.get(persent).localPhotos.get(position);
            boolean s=PhotoChoseActivity.viewModel.setChosePhotos(context,localPhoto.isVideo()?localPhoto.getVideoUrl():localPhoto.getPath());
            photoList.get(persent).localPhotos.get(position).setSelected(s);
            notifyItemChanged(persent);
        }

        @Override
        public void onItemClick(int persent,int position,LocalPhoto localPhoto) {
        }

    };
}
